package com.start.dataSource.service.impl;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.common.exceptions.StartBusException;
import com.start.dataSource.entity.StartDataSource;
import com.start.dataSource.mappers.StartDataSourceMapper;
import com.start.dataSource.service.StartDataSourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.DriverManager;
import java.sql.SQLException;

@Service
@Slf4j
public class StartDataSourceServiceImpl extends ServiceImpl<StartDataSourceMapper, StartDataSource> implements StartDataSourceService {
    @Autowired
    private DynamicRoutingDataSource dynamicRoutingDataSource;
    @Autowired
    private DefaultDataSourceCreator dataSourceCreator;

    @Override
    public void testDataSource(String name, String url, String userName, String pwd) {
        try {
            DriverManager.getConnection(url, userName, pwd);
        } catch (SQLException e) {
            e.printStackTrace();
            StartBusException exception = new StartBusException("获取数据源失败");
            exception.setStackTrace(e.getStackTrace());
            throw exception;
        }
    }

    @Override
    public void saveDs(StartDataSource entity) {
        testDataSource(entity.getName(), entity.getUrl(), entity.getUserName(), entity.getPwd());
        DataSourceProperty dataSourceProperty = createDataSourceProperty(entity);
        DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
        dynamicRoutingDataSource.addDataSource(entity.getKeyFlag(), dataSource);
        save(entity);
    }

    @Override
    public void editorDs(StartDataSource entity) {
        testDataSource(entity.getName(), entity.getUrl(), entity.getUserName(), entity.getPwd());
        StartDataSource oldEntity = getById(entity.getId());
        dynamicRoutingDataSource.removeDataSource(oldEntity.getKeyFlag());
        DataSourceProperty dataSourceProperty = createDataSourceProperty(entity);
        DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
        dynamicRoutingDataSource.addDataSource(entity.getKeyFlag(), dataSource);
        updateById(entity);
    }

    @Override
    public void deleteDs(String id) {
        StartDataSource oldEntity = getById(id);
        dynamicRoutingDataSource.removeDataSource(oldEntity.getKeyFlag());
        removeById(id);
    }


    /**
     * 创建数据源配置
     *
     * @param entity
     * @return
     */
    private DataSourceProperty createDataSourceProperty(StartDataSource entity) {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        dataSourceProperty.setUrl(entity.getUrl());
        dataSourceProperty.setDriverClassName(entity.getDriverClassName());
        dataSourceProperty.setPassword(entity.getPwd());
        dataSourceProperty.setUsername(entity.getUserName());
        return dataSourceProperty;
    }
}
